Проблема: стандартные массивы для очереди могут привести к необходимости дорогостоящих операций сдвига элементов при удалении.
Решение: в книге Algorithms and Data Structures for OOP With C# автор предлагает реализовать очередь на основе связного списка, что позволяет эффективно добавлять элементы в конец и удалять с начала за O(1).
Пример кода:
public class Node<T> { public T Data; public Node<T> Next;
public Node(T data) { Data = data; Next = null; } }
public class QueueLinkedList<T> { private Node<T> front, rear;
public QueueLinkedList() { front = rear = null; }
public void Enqueue(T item) { var newNode = new Node<T>(item); if (rear == null) { front = rear = newNode; return; } rear.Next = newNode; rear = newNode; }
public T Dequeue() { if (front == null) throw new InvalidOperationException("Queue is empty.");
var data = front.Data; front = front.Next;
if (front == null) rear = null;
return data; } }
Преимущества: — Нет затрат на сдвиг элементов — Высокая производительность при операциях добавления и удаления — Универсальная реализация для любых типов данных
Проблема: стандартные массивы для очереди могут привести к необходимости дорогостоящих операций сдвига элементов при удалении.
Решение: в книге Algorithms and Data Structures for OOP With C# автор предлагает реализовать очередь на основе связного списка, что позволяет эффективно добавлять элементы в конец и удалять с начала за O(1).
Пример кода:
public class Node<T> { public T Data; public Node<T> Next;
public Node(T data) { Data = data; Next = null; } }
public class QueueLinkedList<T> { private Node<T> front, rear;
public QueueLinkedList() { front = rear = null; }
public void Enqueue(T item) { var newNode = new Node<T>(item); if (rear == null) { front = rear = newNode; return; } rear.Next = newNode; rear = newNode; }
public T Dequeue() { if (front == null) throw new InvalidOperationException("Queue is empty.");
var data = front.Data; front = front.Next;
if (front == null) rear = null;
return data; } }
Преимущества: — Нет затрат на сдвиг элементов — Высокая производительность при операциях добавления и удаления — Универсальная реализация для любых типов данных
Launched in 2013, Telegram allows users to broadcast messages to a following via “channels”, or create public and private groups that are simple for others to access. Users can also send and receive large data files, including text and zip files, directly via the app.The platform said it has more than 500m active users, and topped 1bn downloads in August, according to data from SensorTower.
The S&P 500 slumped 1.8% on Monday and Tuesday, thanks to China Evergrande, the Chinese property company that looks like it is ready to default on its more-than $300 billion in debt. Cries of the next Lehman Brothers—or maybe the next Silverado?—echoed through the canyons of Wall Street as investors prepared for the worst.